/**
 * Copyright © 2013 Creativor Studio.
 * About license information, please see LICENSE.txt.
 */
package org.creativor.rayson.serialize;

import java.io.IOException;
import java.util.Arrays;

import org.junit.Assert;

/**
 * @author creativor
 * 
 */
public final class Parser {

	private static class TestObject {
		private int i;
		private long l;

		TestObject() {

		}

		TestObject(final int i, final long l) {
			this.i = i;
			this.l = l;
		}

		@Override
		public boolean equals(final Object obj) {
			if (obj == null)
				return false;
			if (!(obj instanceof TestObject))
				return false;
			final TestObject to = (TestObject) obj;
			if (this.i != to.i)
				return false;

			if (this.l != to.l)
				return false;
			return true;
		}

		@Override
		public String toString() {
			return "{i:" + i + ", l:" + l + "}";
		}
	}

	public static Object deserialize(final byte[] bytes) throws SerializeFormatException {
		final Deserializer objDeserializer = new Deserializer(bytes);
		return objDeserializer.deseriaize();
	}

	public static void main(final String[] args) throws IOException, UnsupportedTypeException,
			SerializeFormatException {
		final TestObject obj1 = new TestObject(2, 234);
		final byte[] bytes = serialize(obj1);
		System.out.println(Arrays.toString(bytes));
		final TestObject obj2 = (TestObject) deserialize(bytes);
		Assert.assertEquals(obj1, obj2);
	}

	public static byte[] serialize(final Object obj) throws UnsupportedTypeException {
		final Serializer objSerializer = new Serializer(obj);
		return objSerializer.serialize();
	}

	private Parser() {
		// Forbidden
	}
}